const fetch = require('node-fetch');
const URL_RANKING = 
    "https://api.bilibili.com/x/web-interface/ranking?rid={rid}&day=3";

const URL_GOODS_LIST = 
    "https://ugc.dujia.qunar.com/mm/art/list"

const URL_GOODS_LIST_MAIN = 
    "https://ugc.dujia.qunar.com/mm/art/list?dep=%E5%8C%97%E4%BA%AC&limit=10&channelEnId=&onlyPush=true"

    
const fetchRankingById = (rId) => {
    return fetch(URL_RANKING.replace("{rid}", rId))
        .then(res => res.json())
        .then(json => json)
}

const fetchGoodsList = (page, channelEnId, pushRS) => {
    const search = [
        "dep=%E5%8C%97%E4%BA%AC",
        `page=${page}`,
        "limit=10",
       `channelEnId=${channelEnId}`,
        "gst=false",
        "onlyPush=false"
    ]
    let url = channelEnId == 1 ? `${URL_GOODS_LIST_MAIN}&page=${page}&pushRS=${pushRS}` : (URL_GOODS_LIST + "?" + search.join("&"))
    return fetch(url)
        .then(res => res.json())
        .then(json => json)
}

module.exports = {
    fetchRankingById,
    fetchGoodsList
}